/**
 * chat.js
 */
$(function(){
	//浮动导航
	/*$liu.floatNavigation({
		userURL : [
			{tip:"star主页",url:"http://huaxinkai.imwork.net"},
			{tip:"百度",url:"http://www.baidu.com"},
			{tip:"淘宝",url:"http://www.taobao.com"},
			{tip:"chat",url:"/user/chat.htm"}
			]
	});*/
	
	$(".send").on({
		"touchstart":function(){
			$(this).css({"transform":"translate3D(1px,1px,0px)"});
		},
		"touchend" : function(){
			$(this).css({"transform":"translate3D(0px,0px,0px)"});
			ajaxMsg(function(text){
				sedMessage(text);
			});
		}
	});
	
	$(window).on({
		"keydown":function(e){
			if(e.keyCode == 13){
				e.preventDefault();
				sedMessage();
			} 
		}
	});
	
	var sedMessage = function(text){
		var text = text || $(".input").val().trim(),
		id = "msg_"+new Date().getTime();
		if(text !== ""){
			var div = $("<div><span class='msg_self' id="+ id +">"+text+"</span></div>")
			$(".chat-container").append($(div).fadeIn(1000));
			$(".input").val("");
			showBottom(id);
		}else{
			$liu.msg({msg:"消息不能为空！"});
			return false;
		}
	}
	
	var ajaxMsg = function(fun){
		var url = "http://"+ document.domain + ":8888",
			data = "msg="+new Date().getTime();
		
		console.log("post请求url：",url);
		
		$.get(url,data,function(data,status){
			console.log("post返回：",status,data);
			if(fun){
				fun(data);
			}
		},"text");
	}
	
	var showBottom = function(id){
		document.getElementById(id).scrollIntoView();
	}
	
})